package com.youkeda.meetingroom.controller;

import com.youkeda.meetingroom.model.BookParam;
import com.youkeda.meetingroom.model.BookRecord;
import com.youkeda.meetingroom.model.Result;
import com.youkeda.meetingroom.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RestController
public class BookController {

    @Autowired
    private BookService bookService;
    /**
     * 预约会议室
     * @param record
     * @return
     */
    @RequestMapping(value = "/book", method = RequestMethod.POST)
    public Result book(@RequestBody BookRecord record, HttpSession session) {
        Result result = new Result();
        if(record != null) {
            record.setUserId((String) session.getAttribute("userId"));
            result = bookService.book(record);
        }else{
            result.setSuccess(false);
        }
        return result;
    }
    /**
     * 取消预约会议室
     * @param record
     * @return
     */
    @RequestMapping(value = "/book/cancel", method = RequestMethod.POST)
    public Result cancel(@RequestBody BookRecord record, HttpSession session) {
        Result result = new Result();
        if(record != null){
            record.setUserId((String)session.getAttribute("userId"));
            boolean torf = bookService.cancel(record);
            if(torf){
                result.setSuccess(true);
            }else{
                result.setSuccess(false);
            }
        }else{
            result.setSuccess(false);
        }
        return result;
    }
    /**
     * 检查会议室是否被预约
     * @param bookParam
     * @return
     */
    @RequestMapping(value = "/book/checkBooked", method = RequestMethod.POST)
    public Result checkBooked(@RequestBody BookParam bookParam){
        Result result = new Result();
        if(bookParam != null){
            boolean torf = bookService.checkBooked(bookParam.getMeetingRoomId());
            if(torf){
                result.setSuccess(true);
            }else{
                result.setSuccess(false);
            }
        }else{
            result.setSuccess(false);
        }
        return result;
    }
    /**
     * 获取预约信息
     * @param record
     * @return
     *
     */
    @RequestMapping(value = "/book/getRecordInfo", method = RequestMethod.POST)
    public Result getBookInfo(@RequestBody BookRecord record){
        Result result = new Result();
        if(record != null) {
            result = bookService.getBookInfo(record);
        }else{
            result.setSuccess(false);
        }
        return result;
    }
    /**
     * 签到，即兑现预约
     * 需要检查明文密码
     * @param bookParam
     * @return
     */
    @RequestMapping(value = "/meetingroom/checkIn", method = RequestMethod.POST)
    public Result checkIn(@RequestBody BookParam bookParam){
        Result result = new Result();
        if(bookParam != null) {

            result = bookService.checkIn(bookParam.getUserId(), bookParam.getMeetingRoomId(), bookParam.getPwd());
        }else{
            result.setSuccess(false);
        }
        return result;
    }
    /**
     * 签退
     * @param bookParam
     * @return
     */
    @RequestMapping(value = "/meetingroom/checkOut", method = RequestMethod.POST)
    public Result checkOut(@RequestBody BookParam bookParam) {
        Result result = new Result();
        if(bookParam != null) {
            result = bookService.checkOut(bookParam.getUserId(), bookParam.getMeetingRoomId());
        }else{
            result.setSuccess(false);
        }
        return result;
    }
    /**
     * 获取用户的所有预约记录
     * @return
     */
    @RequestMapping(value = "/book/getRecords", method = RequestMethod.POST)
    public Result getBookRecords(HttpSession session) {
        Result result = new Result();
        String userId = (String) session.getAttribute("userId");
        if (userId != null) {
            result = bookService.getBookRecords(userId);
        }else{
            result.setSuccess(false);
            result.setErrorMessage("未登录");
        }
        return result;
    }

}